import axios
from "../../http/axios.js"
import {
  apiGetServies,
  apiUpdateServiceState
} from "../../http/server.js"
import {
  formatTime
} from "../../utils/util.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    nolist: {
      img: "/assets/img/nolist.png",
      title: "您还没有相关的订单~",
      button: "返回首页 >>"
    },
    btnNav: "", // 用来判断当前打开的弹出框
    dialog: {
      show: false,
      isText: false,
      title: "标题",
      inputValue: "",
      maxlength: 10,
      placeholder: "请输入患者客户端的服务码"
    },
    serviceLists: [{
      id: 1,
      title: "浙江省人民医院",
      state: "服务详情",
      stateIcon: "/assets/",
      list: [{
          img: "",
          name: "打针",
          shortDescription: "肌肉注射,皮下注射",
          price: "199.0"
        },
        {
          img: "",
          name: "打针",
          shortDescription: "肌肉注射,皮下注射",
          price: "199.0"
        }
      ],
      time: "2019年7月19日 12:00~13:00",
      totalPrice: "192.0",
      // text: "预约号 847285",
      button: ["再次预约", "去付款", "评价服务"]
    }], // 数据
    titleIndex: 0, // 头部索引
    titleList: [
      "待服务",
      "进行中",
      "已完成",
      "已取消"
    ] // 头部列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let titleIndex = wx.getStorageSync("titleIndex");
    if (titleIndex) {
      this.setData({
        titleIndex
      })
    }
    this.getServies();
  },
  onHide() {
    wx.removeStorageSync("titleIndex");
  },
  onUnload() {},
  onClickBack() {
    wx.reLaunch({
      url: "/pages/n_index/n_index",
    })
  },
  // 点击弹出框的确认按钮的时候
  onConfirm(e) {
    switch (this.data.dialog.isText) {
      case true: // true是文本提示
        {
          let executeStatus = 1;
          let btnNav = this.data.btnNav;
          if (btnNav === "开始出发") {
            executeStatus = 1;
          } else if (btnNav === "安全到达") {
            executeStatus = 2;
          }
          axios({
            url: apiUpdateServiceState,
            method: "POST",
            data: {
              orderNo: this.data.orderNo,
              executeStatus
            }
          }).then(res => {
            // console.log("触发开始触发，安全到达", res);
            if (btnNav === "开始出发") {
              wx.showToast({
                title: '开始出发了',
                icon: "none",
                duration: 1500,
              })
            } else if (btnNav === "安全到达") {
              wx.showToast({
                title: '安全到达地点了',
                icon: "none",
                duration: 1500
              })
            }
            setTimeout(() => {
              // 无论是开始出发后还是安全到达后都到了进行中的状态
              this.setData({
                titleIndex: 1
              })
              this.getServies()
            }, 1500)
          })
          break;
        }
      case false: // false是输入框
        {
          break;
        }
    }
  },
  // 点击开始出发电话问候等等的button按钮
  onClickBtn(e) {
    let that = this;
    switch (e.detail.item2) {
      case "开始出发":
        {
          let orderNo = e.detail.item.orderNo
          let btnNav = "开始出发";
          let dialog = {
            show: true,
            isText: true,
            title: "确认开始出发", // 提示标题
            text: "如您确认开始出发，我们将记录您的状态信息", // 提示信息
            inputValue: "",
            maxlength: 10,
            placeholder: "请输入患者客户端的服务码"
          }
          this.setData({
            dialog,
            btnNav,
            orderNo // 保存下来方便点击确定的时候
          })
          break;
        }
      case "电话问候":
        {
          wx.makePhoneCall({
            phoneNumber: e.detail.item.hospitalPhone // 医院固定的电话号码
          })
          break;
        }
      case "安全到达":
        {
          let orderNo = e.detail.item.orderNo
          let btnNav = "安全到达";
          let dialog = {
            show: true,
            isText: true,
            title: "确认安全到达",
            text: "如您安全到达，请您点击确认，我们将更好的保障记录您的安全",
            inputValue: "",
            maxlength: 10,
            placeholder: "请输入患者客户端的服务码"
          }
          this.setData({
            dialog,
            btnNav,
            orderNo
          })
          break;
        }
      case "查看评价":
        {
          break;
        }
    }
  },
  // 获取服务列表
  getServies() {
    let orderType = 0;
    switch (Number(this.data.titleIndex)) {
      case 0:
        {
          orderType = 3;
          break;
        }
      case 1:
        {
          orderType = 4
          break;
        }
      case 2:
        {
          orderType = 5
          break;
        }
      case 3:
        {
          orderType = 7 // 传7 后台会查出院内取消和个人取消
          break;
        }
    }
    axios({
      url: apiGetServies,
      data: {
        workersId: wx.getStorageSync("userInfo").workersId,
        pageNum: 1,
        pageSize: 1000,
        orderType
      }
    }).then(res => {
      // 处理返回的数据
      this.processingData(res.records)
    })
  },
  // 处理返回的数据
  processingData(data) {
    let serviceLists = [];
    data.forEach(item => {
      let status = item.status; // 订单状态
      let state = "";
      let buttonList = [];
      let text = "";
      // 公共的往数据中添加 类似享元模式
      function pushList() {
        let listOne = {
          ...item,
          id: item.orderId,
          title: item.patientName,
          state: state,
          stateIcon: "/assets/img/youlan.png",
          list: [{
              img: "",
              name: item.serviceName,
              shortDescription: item.serviceDescribe,
              price: Number(item.servicePrice) / 100
            },
            {
              img: "/assets/img/haocaibao.png",
              name: "耗材包 X " + item.consumablesNum,
              shortDescription: item.consumablesName,
              price: Number(item.consumablesPrice) / 100
            }
          ],
          time: "预约时间 " + formatTime(new Date(item.serviceTime)).replace(/\//g, "-"),
          totalPrice: Number(item.servicePrice) / 100 + (Number(item.consumablesPrice) / 100) * Number(item.consumablesNum),
          text: text,
          button: buttonList,
        };
        serviceLists.push(listOne);
      }
      switch (Number(status)) {
        case 1: // 待支付 不使用
          {
            buttonList = ["去付款"];
            text = "";
            break;
          }
        case 2: // 待分配 不使用
          {
            buttonList = [];
            text = "";
            break;
          }
        case 3: // 待服务
          {
            state = "服务详情";
            buttonList = item.leaveTime ? ["电话问候"] : ["开始出发", "电话问候"]; // 出发时间存在就代表开始出发了
            text = "";
            pushList();
            break;
          }
        case 4: // 进行中
          {
            state = "服务详情";
            buttonList = item.arriveTime ? ["电话问候"] : ["安全到达", "电话问候"]; // 到达时间存在就代表打了了
            text = "";
            pushList()
            break;
          }
        case 5: // 已完成
          {
            state = "服务详情";
            buttonList = ["电话问候"];
            // buttonList = ["查看评价", "电话问候"]; // 评价功能等待开发
            pushList()
            break;
          }
        case 6:
          {
            state = "已关闭"; // 不使用
            break;
          }
        case 7: // 已取消 个人
          {
            state = "服务详情";
            buttonList = ["电话问候"];
            text = "个人取消"
            pushList()
            break;
          }
        case 8: // 已取消 医院
          {
            state = "服务详情";
            buttonList = ["电话问候"]
            text = "院方取消"
            pushList()
            break;
          }
        case 9: // 不使用
          {
            state = "已退款";
            break;
          }
      }
    })
    this.setData({
      serviceLists
    })
  },
  //跳转详情
  onClickToServiceDetail: function(e) {
    let obj = e.detail;
    let item = obj.item;
    let index = obj.index;
    // console.log(item);
    wx.navigateTo({
      url: `/pages/n_serviceDetail/n_serviceDetail?orderNo=${item.orderNo}`,
    })
  },
  //点击头部中的一项
  onClickTabOne: function(e) {
    let obj = e.detail;
    let index = obj.index;
    this.setData({
      titleIndex: index
    })
    this.getServies();
  }
})